//
//  MyInfoViewController.m
//  Beauty
//
//  Created by Snow on 16/9/8.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "MyInfoViewController.h"
#import "GXMacro.h"
#import "DetailNameViewController.h"
#import "DetailGenderViewController.h"
#import "DetailSignViewController.h"

@interface MyInfoViewController ()<SendTextNameDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (nonatomic, strong) UILabel *labelOfTrueName;
@property (nonatomic, strong) UIImageView *image;
@end

@implementation MyInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人资料";
    self.view.backgroundColor = [UIColor colorWithWhite:0.929 alpha:1.000];
    //头像
    UIView *viewOfHeadView = [[UIView alloc] init];
    [self.view addSubview:viewOfHeadView];
    [viewOfHeadView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).mas_equalTo(20);
        make.left.mas_equalTo(self.view.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.height.mas_equalTo(100);
    }];
    UILabel *labelOfHead = [[UILabel alloc] init];
    [viewOfHeadView addSubview:labelOfHead];
    [labelOfHead mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewOfHeadView.mas_top);
        make.left.mas_equalTo(viewOfHeadView.mas_left).mas_equalTo(20);
        make.width.mas_equalTo(50);
        make.bottom.mas_equalTo(viewOfHeadView.mas_bottom);
    }];
    self.image = [[UIImageView alloc] init];
    [viewOfHeadView addSubview:self.image];
    [self.image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewOfHeadView.mas_top).mas_equalTo(10);
        make.right.mas_equalTo(viewOfHeadView.mas_right).mas_equalTo(-10);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(80);
    }];
    viewOfHeadView.backgroundColor = [UIColor whiteColor];
    labelOfHead.text = @"头像";
    self.image.layer.masksToBounds = YES;
    self.image.layer.cornerRadius = 40;
    self.image.image = [UIImage imageNamed:@"logo-1"];
    //手势
    UITapGestureRecognizer *tapPic = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPic:)];
    [viewOfHeadView addGestureRecognizer:tapPic];
    
    //账号
    UIView *viewOfCount = [[UIView alloc] init];
    [self.view addSubview:viewOfCount];
    [viewOfCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewOfHeadView.mas_bottom).mas_equalTo(20);
        make.left.mas_equalTo(self.view.mas_left);
        make.height.mas_equalTo(60);
        make.width.mas_equalTo(self.view.mas_width);
    }];
    viewOfCount.backgroundColor = [UIColor whiteColor];
    
    UILabel *labelOfCount = [[UILabel alloc] init];
    [viewOfCount addSubview:labelOfCount];
    [labelOfCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewOfCount.mas_top);
        make.left.mas_equalTo(viewOfCount.mas_left).mas_equalTo(20);
        make.width.mas_equalTo(150);
        make.bottom.mas_equalTo(viewOfCount.mas_bottom);
    }];
    
    UILabel *labelOfPhoneNumber = [[UILabel alloc] init];
    [viewOfCount addSubview:labelOfPhoneNumber];
    [labelOfPhoneNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewOfCount.mas_top);
        make.right.mas_equalTo(viewOfCount.mas_right).mas_equalTo(-20);
        make.width.mas_equalTo(130);
        make.bottom.mas_equalTo(viewOfCount.mas_bottom);
    }];
    labelOfCount.text = @"玩艺账号";
    labelOfPhoneNumber.text = @"138356026543";
    labelOfPhoneNumber.textAlignment = 2;
    labelOfPhoneNumber.textColor = [UIColor grayColor];
    
    UIView *viewOfLine1 = [[UIView alloc] init];
    [self.view addSubview:viewOfLine1];
    [viewOfLine1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(viewOfCount.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.bottom.mas_equalTo(viewOfCount.mas_top);
        make.height.mas_equalTo(1);
    }];
    viewOfLine1.backgroundColor = [UIColor lightGrayColor];
    
    UIView *viewOfLine2 = [[UIView alloc] init];
    [self.view addSubview:viewOfLine2];
    [viewOfLine2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(viewOfCount.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.top.mas_equalTo(viewOfCount.mas_bottom);
        make.height.mas_equalTo(1);
    }];
    viewOfLine2.backgroundColor = [UIColor lightGrayColor];
    
    //昵称
    UIView *viewOfName = [[UIView alloc] init];
    [self.view addSubview:viewOfName];
    [viewOfName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewOfCount.mas_bottom).mas_equalTo(1);
        make.left.mas_equalTo(self.view.mas_left);
        make.height.mas_equalTo(60);
        make.width.mas_equalTo(self.view.mas_width);
    }];
    viewOfName.backgroundColor = [UIColor whiteColor];
    
    UILabel *labelOfName = [[UILabel alloc] init];
    [viewOfName addSubview:labelOfName];
    [labelOfName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewOfName.mas_top);
        make.left.mas_equalTo(viewOfName.mas_left).mas_equalTo(20);
        make.width.mas_equalTo(150);
        make.bottom.mas_equalTo(viewOfName.mas_bottom);
    }];
    labelOfName.text = @"昵称";
    self.labelOfTrueName = [[UILabel alloc] init];
    [viewOfName addSubview:self.labelOfTrueName];
    [self.labelOfTrueName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewOfName.mas_top);
        make.right.mas_equalTo(viewOfName.mas_right).mas_equalTo(-40);
        make.width.mas_equalTo(130);
        make.bottom.mas_equalTo(viewOfName.mas_bottom);
    }];
    UIImageView *imageOfChar = [[UIImageView alloc] init];
    [viewOfName addSubview:imageOfChar];
    [imageOfChar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewOfName.mas_top).mas_equalTo(20);
        make.right.mas_equalTo(viewOfName.mas_right).mas_equalTo(-10);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    imageOfChar.image = [UIImage imageNamed:@"查看更多"];
    self.labelOfTrueName.text = @"玩艺网_2233";
    self.labelOfTrueName.textAlignment = 2;
    _labelOfTrueName.textColor = [UIColor grayColor];
    UIView *viewOfLine3 = [[UIView alloc] init];
    [self.view addSubview:viewOfLine3];
    [viewOfLine3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(viewOfName.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.top.mas_equalTo(viewOfName.mas_bottom);
        make.height.mas_equalTo(1);
    }];
    viewOfLine3.backgroundColor = [UIColor lightGrayColor];
    
    //手势
    UITapGestureRecognizer *tapName = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapName:)];
    [viewOfName addGestureRecognizer:tapName];
    
    //性别
    UIView *viewOfGender = [[UIView alloc] init];
    [self.view addSubview:viewOfGender];
    [viewOfGender mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewOfName.mas_bottom).mas_equalTo(1);
        make.left.mas_equalTo(self.view.mas_left);
        make.height.mas_equalTo(60);
        make.width.mas_equalTo(self.view.mas_width);
    }];
    viewOfGender.backgroundColor = [UIColor whiteColor];
    
    UILabel *labelOfGender = [[UILabel alloc] init];
    [viewOfGender addSubview:labelOfGender];
    [labelOfGender mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewOfGender.mas_top);
        make.left.mas_equalTo(viewOfGender.mas_left).mas_equalTo(20);
        make.width.mas_equalTo(150);
        make.bottom.mas_equalTo(viewOfGender.mas_bottom);
    }];
    labelOfGender.text = @"性别";
    UILabel *labelOfSex = [[UILabel alloc] init];
    [viewOfGender addSubview:labelOfSex];
    [labelOfSex mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewOfGender.mas_top);
        make.right.mas_equalTo(viewOfGender.mas_right).mas_equalTo(-40);
        make.width.mas_equalTo(130);
        make.bottom.mas_equalTo(viewOfGender.mas_bottom);
    }];
    labelOfSex.text = @"女";
    labelOfSex.textAlignment = 2;
    labelOfSex.textColor = [UIColor grayColor];
    UIView *viewOfLine4 = [[UIView alloc] init];
    [self.view addSubview:viewOfLine4];
    [viewOfLine4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(viewOfGender.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.top.mas_equalTo(viewOfGender.mas_bottom);
        make.height.mas_equalTo(1);
    }];
    viewOfLine4.backgroundColor = [UIColor lightGrayColor];
    UIImageView *imageOfSex = [[UIImageView alloc] init];
    [viewOfGender addSubview:imageOfSex];
    [imageOfSex mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewOfGender.mas_top).mas_equalTo(20);
        make.right.mas_equalTo(viewOfGender.mas_right).mas_equalTo(-10);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    imageOfSex.image = [UIImage imageNamed:@"查看更多"];
    //手势
    UITapGestureRecognizer *tapGender = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGender:)];
    [viewOfGender addGestureRecognizer:tapGender];
    //签名
    UIView *viewOfSign = [[UIView alloc] init];
    [self.view addSubview:viewOfSign];
    [viewOfSign mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewOfGender.mas_bottom).mas_equalTo(1);
        make.left.mas_equalTo(self.view.mas_left);
        make.height.mas_equalTo(60);
        make.width.mas_equalTo(self.view.mas_width);
    }];
    viewOfSign.backgroundColor = [UIColor whiteColor];
    
    UILabel *labelOfSign = [[UILabel alloc] init];
    [viewOfSign addSubview:labelOfSign];
    [labelOfSign mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewOfSign.mas_top);
        make.left.mas_equalTo(viewOfSign.mas_left).mas_equalTo(20);
        make.width.mas_equalTo(100);
        make.bottom.mas_equalTo(viewOfSign.mas_bottom);
    }];
    labelOfSign.text = @"签名";
    UILabel *labelOfSignIn = [[UILabel alloc] init];
    [viewOfSign addSubview:labelOfSignIn];
    [labelOfSignIn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewOfSign.mas_top);
        make.right.mas_equalTo(viewOfSign.mas_right).mas_equalTo(-40);
        make.width.mas_equalTo(250);
        make.bottom.mas_equalTo(viewOfSign.mas_bottom);
    }];
    
    UIImageView *imageOfSign = [[UIImageView alloc] init];
    [viewOfSign addSubview:imageOfSign];
    [imageOfSign mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewOfSign.mas_top).mas_equalTo(20);
        make.right.mas_equalTo(viewOfSign.mas_right).mas_equalTo(-10);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    imageOfSign.image = [UIImage imageNamed:@"查看更多"];
    
    labelOfSignIn.text = @"我还没想好说什么";
    labelOfSignIn.textColor = [UIColor grayColor];
    labelOfSignIn.textAlignment = 2;
    UIView *viewOfLine5 = [[UIView alloc] init];
    [self.view addSubview:viewOfLine5];
    [viewOfLine5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(viewOfSign.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.top.mas_equalTo(viewOfSign.mas_bottom);
        make.height.mas_equalTo(1);
    }];
    viewOfLine5.backgroundColor = [UIColor lightGrayColor];
    //手势
    UITapGestureRecognizer *tapSign = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSign:)];
    [viewOfSign addGestureRecognizer:tapSign];
}
- (void)tapName:(UITapGestureRecognizer *)tap
{
    DetailNameViewController *name = [[DetailNameViewController alloc] init];
    name.placeHolder = _labelOfTrueName.text;
    name.delegate = self;
    [self.navigationController pushViewController:name animated:YES];
    
}
//delelgate
- (void)sendTextName:(NSString *)string
{
    _labelOfTrueName.text = string;
}
- (void)tapPic:(UITapGestureRecognizer *)tap
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"设置头像" message:@"拍照或者从相册中选择照片" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * __nonnull action) { // 点击了拍照按钮"
        
        // 拍照
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
        if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
        {
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            
            picker.allowsEditing = YES;
            picker.sourceType = sourceType;
            [self presentViewController:picker animated:YES completion:nil];
        }else
        {
            NSLog(@"模拟其中无法打开照相机,请在真机中使用");
        }
        
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * __nonnull action) { // 点击了相册按钮
        
        
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { // 没有相册
            return;
        }
        
        
        UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
        pickerVC.delegate = self;
        
        pickerVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:pickerVC animated:YES completion:nil];
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * __nonnull action) {
        
    }];
    
    [alertController addAction:action1];
    [alertController addAction:action2];
    [alertController addAction:action3];
    
    [self presentViewController:alertController animated:YES completion:nil];

}
- (void)tapGender:(UITapGestureRecognizer *)tap
{
    DetailGenderViewController *gender = [[DetailGenderViewController alloc] init];
    [self.navigationController pushViewController:gender animated:YES];
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(nonnull UIImagePickerController *)picker didFinishPickingMediaWithInfo:(nonnull NSDictionary<NSString *,id> *)info
{
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    
    if ([type isEqualToString:@"public.image"])
    {
        
        UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        NSData *data;
        if (UIImagePNGRepresentation(image) == nil)
        {
            data = UIImageJPEGRepresentation(image, 1.0);
        }
        else
        {
            data = UIImagePNGRepresentation(image);
        }
        
        
        NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
        
        
        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        
        [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
        NSString *imageName = [NSString stringWithFormat:@"/headerImage.png"];
        
        [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:imageName] contents:data attributes:nil];
        //        [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];
        
        //得到沙盒中图片的完整路径
        NSString *filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath,  imageName];
        image = [UIImage imageWithContentsOfFile:filePath];
        
        //        NSData * imageData = UIImageJPEGRepresentation(image,1);
        //        NSInteger length = [imageData length]/1000;
        //        NSLog(@"%ld",length);
        
        //关闭相册界面
        [picker dismissViewControllerAnimated:YES completion:nil];
        
        [self.image setImage:image];
    }
}

// 用户取消了操作
- (void)imagePickerControllerDidCancel:(nonnull UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)tapSign:(UITapGestureRecognizer *)tap
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
